package com.tnovoselec.android.travelassistant.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.tnovoselec.android.travelassistant.dto.CurrencyDto;
import com.tnovoselec.android.travelassistant.dto.CurrencyResultHolder;
import com.tnovoselec.android.travelassistant.dto.GasPriceDto;

public class ExchangeRatesParser extends DefaultHandler {

	private StringBuilder buffer;

	private final static String TAG_DATE = "Date";

	private final static String TAG_CURRENCY = "Currency";
	private final static String TAG_NAME = "Name";
	private final static String TAG_UNIT = "Unit";
	private final static String TAG_BUY_RATE_CACHE = "BuyRateCache";
	private final static String TAG_BUY_RATE_FOREIGN = "BuyRateForeign";
	private final static String TAG_MEAN_RATE = "MeanRate";
	private final static String TAG_SELL_RATE_FOREIGN = "SellRateForeign";
	private final static String TAG_SELL_RATE_CACHE = "SellRateCache";
	private final static String TAG_FLAG_IMAGE_URL = "image";

	private final static String ATTR_CODE = "Code";

	private boolean inDate = false;
	private boolean inCurrency = false;
	private boolean inName = false;
	private boolean inUnit = false;
	private boolean inBuyRateCache = false;
	private boolean inBuyRateForeign = false;
	private boolean inMeanRate = false;
	private boolean inSellRateForeign = false;
	private boolean inSellRateCache = false;
	private boolean inFlagImageUrl = false;

	private CurrencyDto tempCurrency;
	private List<CurrencyDto> rates;

	@Override
	public void startDocument() throws SAXException {
		rates = new ArrayList<CurrencyDto>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

		buffer = new StringBuilder("");
		if (localName.equalsIgnoreCase(TAG_DATE)) {
			inDate = true;
		} else if (localName.equalsIgnoreCase(TAG_CURRENCY)) {
			inCurrency = true;
			tempCurrency = new CurrencyDto();
			if (attributes.getValue(ATTR_CODE) != null) {
				tempCurrency.setCode(attributes.getValue(ATTR_CODE));
			}
		} else if (localName.equalsIgnoreCase(TAG_NAME)) {
			inName = true;
		} else if (localName.equalsIgnoreCase(TAG_UNIT)) {
			inUnit = true;
		} else if (localName.equalsIgnoreCase(TAG_BUY_RATE_CACHE)) {
			inBuyRateCache = true;
		} else if (localName.equalsIgnoreCase(TAG_BUY_RATE_FOREIGN)) {
			inBuyRateForeign = true;
		} else if (localName.equalsIgnoreCase(TAG_MEAN_RATE)) {
			inMeanRate = true;
		} else if (localName.equalsIgnoreCase(TAG_SELL_RATE_FOREIGN)) {
			inSellRateForeign = true;
		} else if (localName.equalsIgnoreCase(TAG_SELL_RATE_CACHE)) {
			inSellRateCache = true;
		} else if (localName.equalsIgnoreCase(TAG_FLAG_IMAGE_URL)) {
			inFlagImageUrl = true;
		}

	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if (inDate) {
			CurrencyResultHolder.dateOfPublish = buffer.toString();
		} else if (inCurrency) {
			if (inName) {
				tempCurrency.setName(buffer.toString());
			} else if (inUnit) {
				tempCurrency.setUnit(Integer.parseInt(buffer.toString().replace(",", ".")));
			} else if (inBuyRateCache) {
				tempCurrency.setBuyRateCache(Float.parseFloat(buffer.toString().replace(",", ".")));
			} else if (inBuyRateForeign) {
				tempCurrency.setBuyRateForeign(Float.parseFloat(buffer.toString().replace(",", ".")));
			} else if (inMeanRate) {
				tempCurrency.setMeanRate(Float.parseFloat(buffer.toString().replace(",", ".")));
			} else if (inSellRateForeign) {
				tempCurrency.setSellRateForeign(Float.parseFloat(buffer.toString().replace(",", ".")));
			} else if (inSellRateCache) {
				tempCurrency.setSellRateCache(Float.parseFloat(buffer.toString().replace(",", ".")));
			} else if (inFlagImageUrl) {
				tempCurrency.setImageFlagUrl(buffer.toString());
			}
		}

		if (localName.equalsIgnoreCase(TAG_DATE)) {
			inDate = false;
		} else if (localName.equalsIgnoreCase(TAG_CURRENCY)) {
			inCurrency = false;
			rates.add(tempCurrency);
		} else if (localName.equalsIgnoreCase(TAG_NAME)) {
			inName = false;
		} else if (localName.equalsIgnoreCase(TAG_UNIT)) {
			inUnit = false;
		} else if (localName.equalsIgnoreCase(TAG_BUY_RATE_CACHE)) {
			inBuyRateCache = false;
		} else if (localName.equalsIgnoreCase(TAG_BUY_RATE_FOREIGN)) {
			inBuyRateForeign = false;
		} else if (localName.equalsIgnoreCase(TAG_MEAN_RATE)) {
			inMeanRate = false;
		} else if (localName.equalsIgnoreCase(TAG_SELL_RATE_FOREIGN)) {
			inSellRateForeign = false;
		} else if (localName.equalsIgnoreCase(TAG_SELL_RATE_CACHE)) {
			inSellRateCache = false;
		} else if (localName.equalsIgnoreCase(TAG_FLAG_IMAGE_URL)) {
			inFlagImageUrl = false;
		}

	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		buffer.append(String.valueOf(ch, start, length));
	}
	
	public List<CurrencyDto> getRates(){
		return rates;
	}

}
